package com.JSON;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 从 JSON 对象到 Java 变量的解码过程如下
 *
 * @author shkstart
 * @creat 2022-05-16:54
 */
public class JSONParseTest {
    public static void main(String[] args) {
        JSONObject object = JSON
                .parseObject("{\"boolean\":true,\"string\":\"string\",\"list\":[1,2,3],\"int\":2}");
        //string
        String s = object.getString("string");
        System.out.println(s);
        //int
        int i = object.getInteger("int");
        System.out.println(i);
        //boolean
        boolean b = object.getBoolean("boolean");
        System.out.println(b);
        //list
        List<Integer> integers = JSON.parseArray(object.getJSONArray("list").toJSONString(), Integer.class);
        integers.forEach(System.out::println);
        //null
        System.out.println(object.getString("null"));

        //从字符串解析JSON对象
        JSONObject obj = JSON.parseObject("{\"runoob\":\"菜鸟教程\"}");
        System.out.println(obj);
        //从字符串解析JSON数组
        JSONArray arr = JSON.parseArray("[\"菜鸟教程\",\"RUNOOB\"]\n");
        System.out.println(arr);
        //将JSON对象转化为字符串
        String objStr = JSON.toJSONString(obj);
        System.out.println(objStr);
        //将JSON数组转化为字符串
        String arrStr = JSON.toJSONString(arr);
        System.out.println(arrStr);
    }


}
